package net.lainiao.reptile;

import net.lainiao.reptile.jobs.ReptileIndexJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * Created by Administrator on 2017/1/9.
 */
public class IndexService {
    static  SchedulerFactory  factory=new StdSchedulerFactory();
    static Scheduler scheduler = null;
    public static volatile int status = 0;
    protected static void stopInfn() {
       if(IndexService.scheduler!=null){
           try {
               IndexService.scheduler.shutdown(false);
           } catch (SchedulerException e) {
               e.printStackTrace();
           }
       }
    }
    public static synchronized  String start(){
        if(status==0){
            status=1;
            startInfo();
            return "服务启动成功";
        }
        return "服务已经在运行";
    }

    private static void startInfo() {

        try {
            scheduler = factory.getScheduler();
            JobDetail job = JobBuilder.newJob(ReptileIndexJob.class).withIdentity("modeinfo", "index").build();
            Trigger trigger = TriggerBuilder
                    .newTrigger()
                    .withIdentity("modeinfo", "index")
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?"))
                    .build();
            scheduler.scheduleJob(job, trigger);
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public static synchronized  String stop(){
        if(status!=0){
            status=0;
            stopInfn();
            return "服务停止成功";
        }
        return "服务早前已经被终止";
    }
}
